package com.android.sdcard.folder;

import java.io.File;
import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.android.sdcard.folder.FolderComponent.Type;
import com.bhprod.guitartrack.MainActivity;
import com.bhprod.guitartrack.R;

public class ListFolderActivity extends ListActivity {

	private File currentDir;
	private FileArrayAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		currentDir = new File(Environment.getExternalStorageDirectory().getPath());
		fill(currentDir);
	}

	private void fill(File f) {
		File[] dirs = f.listFiles();
		this.setTitle(getString(R.string.current_dir) + ": " + f.getName());

		List<FolderComponent> directories = new ArrayList<FolderComponent>();
		List<FolderComponent> files = new ArrayList<FolderComponent>();

		try {
			for (File ff : dirs) {
				Date lastModDate = new Date(ff.lastModified());
				DateFormat formater = DateFormat.getDateTimeInstance();
				String date_modify = formater.format(lastModDate);

				if (ff.isDirectory()) {

					File[] fbuf = ff.listFiles();
					int buf = 0;
					if (fbuf != null) {
						buf = fbuf.length;
					} else
						buf = 0;
					String num_item = String.valueOf(buf);
					if (buf == 0)
						num_item = num_item + " " + getString(R.string.item);
					else
						num_item = num_item + " " + getString(R.string.items);

					directories.add(new FolderComponent(ff.getName(), num_item, date_modify, ff.getAbsolutePath(),
							Type.FOLDER));
				} else {
					// TODO : Display only compatible music files
					
					if (ff.getName().endsWith("mp3")) {
						files.add(new FolderComponent(ff.getName(), ff.length() + " " + getString(R.string.bytes), date_modify, ff
								.getAbsolutePath(), Type.FILE));
					}
				}
			}
		} catch (Exception e) {
			Log.e("ListFolderActivity", "Error", e);
		}

		Collections.sort(directories);
		Collections.sort(files);
		directories.addAll(files);
		// In root folder, don't display up button 
		if (!f.getName().equalsIgnoreCase("sdcard")) {
			directories.add(0, new FolderComponent("..", getString(R.string.parent_dir), "", f.getParent(), Type.UP));
		}
		adapter = new FileArrayAdapter(ListFolderActivity.this, R.layout.file_view, directories);
		this.setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		FolderComponent o = adapter.getItem(position);
		if (Type.FOLDER.equals(o.getType()) || Type.UP.equals(o.getType())) {
			currentDir = new File(o.getPath());
			fill(currentDir);
		}
		if (Type.FILE.equals(o.getType())) {
			// Launch main activity with input file and close this one
			Intent intent = new Intent(this, MainActivity.class);
			String path = o.getPath();
			if (path.startsWith("/storage")) {
				path = path.replace("/storage", "");
			}
			intent.putExtra("file", o.getPath());
			startActivity(intent);
			finish();
		}
	}

}
